package com.bawei.es.controller;

import com.bawei.common.core.domain.R;
import com.bawei.es.service.EsIndexService;
import com.bawei.system.domain.IndexCreateRequest;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.PostConstruct;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/esIndex")
@Api(value = "ES索引管理",tags = "ES索引管理")
public class EsIndexController {

    @Autowired
    private EsIndexService esIndexService;

    /**
     * 判断索引是否存在
     *
     * @return
     */
    @GetMapping("/exits")
    @ApiOperation(value = "判断索引是否存在")
    public R helloWord(@RequestParam("indexName") String indexName) {
        return R.ok(esIndexService.exits(indexName));
    }


    /**
     * 根据索引名称创建索引
     *
     * @return
     */
    @PostMapping("/create")
    @ApiOperation(value = "根据索引名称创建索引")
    public R<Boolean> indexCreate(@RequestBody IndexCreateRequest request) {
        return esIndexService.indexCreate(request);
    }

    /**
     * 根据索引名称创建索引
     *
     * @return
     */
    @GetMapping("/request")
    @ApiOperation(value = "创建索引")
    public R indexRequest(@RequestBody IndexCreateRequest request) {
        esIndexService.indexRequest(request);
        return R.ok();
    }


    /**
     * 根据索引名称、映射结构创建索引
     *
     * @return
     */
    @GetMapping("/createWithMapping")
    @ApiOperation(value = "根据索引名称、映射结构创建索引")
    public R<Boolean> indexCreateWithMapping(@RequestParam("indexName") String indexName,@RequestParam("indexMapping") String indexMapping) {

        return esIndexService.indexCreateWithMapping(indexName,indexMapping);
    }

    /**
     * 根据索引名称查询索引是否存在
     *
     * @return
     */
    @GetMapping("/indexExit")
    @ApiOperation(value = "根据索引名称查询索引是否存在")
    public R<Boolean> indexExit(@RequestParam("indexName") String indexName) {

        return esIndexService.indexExit(indexName);
    }

    /**
     * 根据索引名称删除索引
     *
     * @return
     */
    @GetMapping("/indexDelete")
    @ApiOperation(value = "根据索引名称删除索引")
    public R<Boolean> indexDelete(@RequestParam("indexName") String indexName) {

        return esIndexService.indexDelete(indexName);
    }

    /**
     * 根据索引名称获取mapping
     *
     * @return
     */
    @GetMapping("/indexGetMapping")
    @ApiOperation(value = "根据索引名称获取mapping")
    public R<Map> indexGetMapping(@RequestParam("indexName") String indexName) {

        return esIndexService.indexGetMapping(indexName);
    }


    /**
     * 查询所有索引名称
     * @return
     */
    @GetMapping("/selectAllIndex")
    @ApiOperation(value = "查询所有索引名称")
    public R<List<String>> selectAllIndex() {
        return R.ok(esIndexService.selectAllIndex());
    }

}
